#include "CalibrateWindow.h"
#include "assert.h"
#include "gloveCommands.h"

namespace vkbrd
{
	

	CalibrateWindow::CalibrateWindow( QWidget* _parent ) : 
QDialog(_parent)
	{
		ui.setupUi(this);

		currentSeqNum = 0;
		
		for (int i = 0; i < numGloves * fingerCount; ++i)
		{
			BoolFingers seq;
			seq.set(i, true);
			sequencesToConfigure.append(seq);
		}

		sliders << CalibrationSliders(ui.verticalSlider_0_Min, ui.progressBar_0, ui.verticalSlider_0_Max) << 
			CalibrationSliders(ui.verticalSlider_1_Min, ui.progressBar_1, ui.verticalSlider_1_Max) << 
			CalibrationSliders(ui.verticalSlider_2_Min, ui.progressBar_2, ui.verticalSlider_2_Max) << 
			CalibrationSliders(ui.verticalSlider_3_Min, ui.progressBar_3, ui.verticalSlider_3_Max) << 
			CalibrationSliders(ui.verticalSlider_4_Min, ui.progressBar_4, ui.verticalSlider_4_Max) << 
			CalibrationSliders(ui.verticalSlider_5_Min, ui.progressBar_5, ui.verticalSlider_5_Max) << 
			CalibrationSliders(ui.verticalSlider_6_Min, ui.progressBar_6, ui.verticalSlider_6_Max) << 
			CalibrationSliders(ui.verticalSlider_7_Min, ui.progressBar_7, ui.verticalSlider_7_Max) << 
			CalibrationSliders(ui.verticalSlider_8_Min, ui.progressBar_8, ui.verticalSlider_8_Max) << 
			CalibrationSliders(ui.verticalSlider_9_Min, ui.progressBar_9, ui.verticalSlider_9_Max);
	}

	void CalibrateWindow::calibratePressed()
	{
		emit calibrateSequence(sequencesToConfigure[currentSeqNum]);
		++currentSeqNum;
		currentSeqNum %= sequencesToConfigure.count();
		nextScreen();
	}

	void CalibrateWindow::nextScreen()
	{
		BoolFingers currentSequence = sequencesToConfigure[currentSeqNum];
		QString message;
		int fingersToBend = currentSequence.flagsSetCount();

		assert(fingersToBend && "aaaaa!"); //:)

		if (fingersToBend == 1)
			message.append(tr("Please bend finger: "));
		else
			message.append(tr("Please bend fingers: "));

		for (int i = 0, j=0; 
			i < dFingerCount && j < fingersToBend;
			++i)
			if (currentSequence(i))
			{
				message.append(QString::number(i));
				++j;
			}

		message.append(tr("\n and press Next."));
		ui.userMessage->setText(message);
	}

	void CalibrateWindow::calibrationChanged( int _gloveId, FingerCalibration _calibration )
	{
		int fingerIndex = _gloveId * fingerCount;

		for (int i = 0; i < fingerCount; ++i, ++fingerIndex)
		{
			sliders[fingerIndex].minValue->setValue(_calibration.minMax[i].minPress);
			sliders[fingerIndex].maxValue->setValue(_calibration.minMax[i].maxPress);
		}
	}
}//namespace vkbrd